<template>
  <el-dialog v-model="dialog.visible" :title="title" width="30%" @close="handleCloseDialog">
    <div class="form-container">
      <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        label-width="100"
        class="form mb-0"
      >
        <el-form-item label="品牌编码">
          <el-input v-model.trim="formData.brandCode" placeholder="请输入品牌编码" />
        </el-form-item>
        <el-form-item label="品牌名称" prop="brandName">
          <el-input v-model.trim="formData.brandName" placeholder="请输入品牌名称" />
        </el-form-item>
        <el-form-item label="Logo" prop="brandLogo">
          <single-image-upload
            v-model="formData.brandLogo"
            :max-file-size="5"
            @loading="handleLoading"
          ></single-image-upload>
        </el-form-item>
        <el-form-item label="品牌简介" prop="brandIntroduction">
          <el-input v-model.trim="formData.brandIntroduction" placeholder="请输入品牌简介" />
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="handleSubmit" :loading="loading">确 定</el-button>
        <el-button @click="handleCloseDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup lang="ts">
import { ElMessage } from "element-plus";
import brandApi, { SysBrandForm } from "@/api/brand/brand-api";
// 定义 emit，用于通知外部关闭事件
const emit = defineEmits(["update:modelValue", "loadData"]);
const dialog = reactive({
  title: "",
  visible: false,
});
/*基础信息*/
const formRef = ref();
const loading = ref(false);
const title = computed(() => (formData.value.id ? "编辑" : "新增"));
const defaultFormData: SysBrandForm = {
  address: "",
  id: undefined,
  mallName: "",
  phonenumber: "",
  remark: "",
};
const formData = ref({
  ...defaultFormData,
});
const formRules = reactive({
  // brandCode: [{ required: true, message: "请输入品牌编码", trigger: "blur" }],
  brandName: [{ required: true, message: "请输入品牌名称", trigger: "blur" }],
});

async function handleSubmit() {
  const validate = await formRef.value.validate();
  if (!validate) return;
  try {
    loading.value = true;
    if (formData.value.id) {
      await brandApi.update(formData.value);
    } else {
      await brandApi.add(formData.value);
    }
    ElMessage.success("成功");
    emit("loadData");
    handleCloseDialog();
  } catch (error) {
    console.log("error", error);
  } finally {
    loading.value = false;
  }
}

// 关闭弹窗
function handleCloseDialog() {
  formData.value = {};
  Object.assign(formData.value, defaultFormData);
  dialog.visible = false;
  emit("update:modelValue", false); // 关闭弹窗
}

function showModal(val) {
  dialog.visible = true;
  Object.assign(formData.value, defaultFormData, val);
}

function handleLoading(val) {
  loading.value = val;
}

defineExpose({
  showModal,
});
</script>
<style style="scss" scoped>
.form-container {
  height: 500px;
  overflow: auto;
}
.form {
  ::v-deep(.el-form-item) {
    margin-bottom: 0;
  }
}
</style>
